package com.fxz.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxz.common.dto.SysMenuDto;
import com.fxz.entity.SysMenu;
import com.fxz.entity.SysUser;
import com.fxz.mapper.SysMenuMapper;
import com.fxz.mapper.SysUserMapper;
import com.fxz.service.SysMenuService;
import com.fxz.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fxz
 * @since 2021-05-04
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 获取当前用户导航栏
     *
     * @return
     */
    @Override
    public List<SysMenuDto> getCurrentUserNav() {
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser sysUser = sysUserService.getByUserName(username);
        List<Long> menuIds = sysUserMapper.geNavMenuIds(sysUser.getId());
        List<SysMenu> menus = this.listByIds(menuIds);
        //转树状结构
        List<SysMenu> menuTree = buildTreeMenu(menus);
        //实体转DTO
        return convert(menuTree);
    }

    @Override
    public List<SysMenu> tree() {
        //获取所有菜单信息
        List<SysMenu> menus = this.list(new LambdaQueryWrapper<SysMenu>().orderByAsc(SysMenu::getOrderNum));
        //将菜单信息转成树状结构

        return buildTreeMenu(menus);
    }

    /**
     * 实体转DTO
     *
     * @param menuTree
     * @return
     */
    private List<SysMenuDto> convert(List<SysMenu> menuTree) {
        List<SysMenuDto> menuDtos = new ArrayList<>();
        menuTree.forEach(item -> {
            SysMenuDto dto = new SysMenuDto();
            dto.setId(item.getId());
            dto.setName(item.getPerms());
            dto.setTitle(item.getName());
            dto.setComponent(item.getComponent());
            dto.setPath(item.getPath());
            if (!CollectionUtils.isEmpty(item.getChildren())) {
                //子节点调用当前方法 进行再次转化
                dto.setChildren(convert(item.getChildren()));
            }
            menuDtos.add(dto);
        });
        return menuDtos;
    }

    /**
     * 构建树状结构
     *
     * @param menus 导航栏菜单
     * @return
     */
    private List<SysMenu> buildTreeMenu(List<SysMenu> menus) {

        List<SysMenu> finalMenus = new ArrayList<>();
        //找到各自的子节点
        for (SysMenu menu : menus) {
            for (SysMenu e : menus) {
                if (menu.getId().equals(e.getParentId())) {
                    List<SysMenu> children = menu.getChildren();
                    children.add(e);
                }
            }
            //提取父节点
            if (menu.getParentId().equals(0L)) {
                finalMenus.add(menu);
            }
        }
        System.out.println(JSONUtil.toJsonStr(finalMenus));
        return finalMenus;
    }
}
